'use strict';

// 1.控制器是否存在
// 2.方法是否存在
// 3.成功响应处理
// 4.失败响应处理

describe('fbdApp file controller', function() {
  var $rootScope, $controller, $scope;
  var $upload;
  var $q;

  beforeEach(module('fbdApp.uploadModule'));
  beforeEach(inject(function(_$controller_, _$rootScope_, _$q_, _Upload_) {
    $rootScope = _$rootScope_;
    $scope = _$rootScope_.$new();
    $upload = _Upload_;
    $controller = _$controller_;
    $q = _$q_;
  }));

  function createController() {
    return $controller('fileCtrl', {
      $scope: $scope,
      Upload: $upload
    });
  }

  it('should be defined', function(done) {
    var controller = createController();
    expect(controller).not.toBe(null);
    expect($scope.uploadFile).toBeDefined();
    return done();
  });

  describe('upload method', function() {
    it('success response', function(done) {
      var controller = createController();
      var deferred = $q.defer();
      var fakedData = {
        data: 'ebafd23423cfab.fbd',
        status: 200,
        config: Object,
        statusText: 'OK'
      };
      deferred.resolve(fakedData);

      spyOn($upload, 'upload').and.returnValue(deferred.promise);
      $scope.uploadFile('');
      $scope.$apply();

      expect($scope.isUploaded).toBe(true);
      expect($scope.uploadInfo).toBe('上传成功！')
      expect($scope.fileName).toBe('ebafd23423cfab.fbd');
      return done();
    });

    it('error response', function(done) {
      var controller = createController();
      var deferred = $q.defer();
      var fakedData = {
        data: '参数错误',
        status: 400
      };
      deferred.reject(fakedData);
      spyOn($upload, 'upload').and.returnValue(deferred.promise);
      $scope.uploadFile('');
      $scope.$apply();

      expect($scope.isUploaded).toBe(false);
      expect($scope.uploadInfo).toBe('上传失败！原因：参数错误');
      return done();
    });
  });
});
